<?php
class editor_tiles
{
    public $id;
    public $html;

    public $cats = false;
    public $catoption = null;

    function _print($txt)
    {
        $this->html.=$txt."\n";
    }

    function  __construct($id = 0) {
        $this->id = (int)$id;
        $this->write();
        $this->_print('<style>
.textf{width:100px}
td{border:1px solid #eee;}
</style>');
    }

    function write()
    {
        if(isset($_POST['save']))
        {
            
            foreach($_POST['id'] as $id)
            {
                if($id == 0 && $_POST['posX'][0])
                {
                    $query = 'INSERT INTO `admin__tiles` SET
`name`=\''.mysql_escape_string($_POST['name'][0]).'\',
`posX`=\''.mysql_escape_string($_POST['posX'][0]-1).'\',
`posY`=\''.mysql_escape_string($_POST['posY'][0]-1).'\',
`asset`=\''.mysql_escape_string($_POST['asset'][0]).'\',
`cat`=\''.mysql_escape_string($_POST['cat'][0]).'\'';
                }
                else if($id)
                {
                $query = 'INSERT INTO `admin__tiles` SET
`id`=\''.mysql_escape_string($_POST['id'][$id]).'\',
`name`=\''.mysql_escape_string($_POST['name'][$id]).'\',
`posX`=\''.mysql_escape_string($_POST['posX'][$id]-1).'\',
`posY`=\''.mysql_escape_string($_POST['posY'][$id]-1).'\',
`asset`=\''.mysql_escape_string($_POST['asset'][$id]).'\',
`cat`=\''.mysql_escape_string($_POST['cat'][$id]).'\'
                    ON DUPLICATE KEY UPDATE
`id`=\''.mysql_escape_string($_POST['id'][$id]).'\',
`name`=\''.mysql_escape_string($_POST['name'][$id]).'\',
`posX`=\''.mysql_escape_string($_POST['posX'][$id]-1).'\',
`posY`=\''.mysql_escape_string($_POST['posY'][$id]-1).'\',
`asset`=\''.mysql_escape_string($_POST['asset'][$id]).'\',
`cat`=\''.mysql_escape_string($_POST['cat'][$id]).'\'
';
                }
              
                _database::query($query);

            }
        }
    }

    function getTileList()
    {
        $query = 'SELECT * FROM `admin__tiles` LIMIT 100';
        return _database::toarray($query);
    }

    function tooption($cat_arr)
    {
        if(isset($this->catoption[$cat_arr['id']]))
        return $this->catoption[$cat_arr['id']];
        $out = '';
        foreach($this->cats as $cat)
        {
            if($cat_arr['id'] != $cat['id'])
            {
                $out.='<option value="'.$cat['id'].'">'.$cat['name'].'</option>';
            }
            else
            {
                $out.='<option selected="selected" value="'.$cat['id'].'">'.$cat['name'].'</option>';
            }
        }

        $this->catoption[$cat_arr['id']] = $out;
        return $this->catoption[$cat_arr['id']];
    }

    function drawcat($cat)
    {
        if($this->cats)
        {
            return $this->tooption($this->cats[$cat]);
        }
        $query = 'SELECT * FROM `admin__tiles_cat` ORDER BY `name` DESC';
        $cats = _database::toarray($query);
        foreach($cats as $row)
        {
            $this->cats[$row['id']] = $row;
        }

        return $this->tooption($this->cats[$cat]);
    }

    function print_tile($tile)
    {

        $this->_print('<tr>');
        $this->_print('<td valign="top" style="width:107px;height:72px;background:url(/img/tiles/'.$tile['asset'].'.png) '.(-106*$tile['posX']).'px '.(-70*$tile['posY']).'px no-repeat">&#160;</td>');
                                        $this->_print('<td valign="top"><input style="width:200px" name="name['.(int)$tile['id'].']" value="'.$tile['name'].'" /></td>');
                                        $this->_print('<td valign="top"><input style="width:50px" name="posX['.(int)$tile['id'].']" value="'.($tile['posX']+1).'"/></td>');
                                        $this->_print('<td valign="top"><input style="width:50px" name="posY['.(int)$tile['id'].']" value="'.($tile['posY']+1).'"/></td>');
                                        $this->_print('<td valign="top"><input style="width:100px" name="asset['.(int)$tile['id'].']" value="'.$tile['asset'].'"/>');
                                        $this->_print('<div><a href="http://world.ljrate.ru/img/tiles/'.$tile['asset'].'.png">'.$tile['asset'].'</a></div></td>');
                                        $this->_print('<td valign="top"><select style="width:250px" name="cat['.(int)$tile['id'].']">'.$this->drawcat($tile['cat']).'</select></td>');
                                        $this->_print('<td valign="top"><input type="hidden" name="id['.(int)$tile['id'].']" value="'.$tile['id'].'" /><input style="width:50px" type="submit" name="delete['.$tile['id'].']" value="delete"/></td>');
                                        $this->_print('</tr>');

                                    }

                                    function out()
                                    {
                                        $this->_print('<!-- tile '.$this->id.' -->');
                                        // рисуем поле
                                        $tiles = $this->getTileList();
                                        $this->_print('<table cellpadding="0" cellspacing="0" border="0" >');
                                        $this->_print('<form method="post">');
                                        $this->_print('<tr>');
                                        $this->_print('<td width="107px"  valign="top" >&#160;</td>');
                                        $this->_print('<td width="300px" valign="top"><b>название</td>');
                                        $this->_print('<td width="100px" valign="top"><b>позиция X в файле</td>');
                                        $this->_print('<td width="100px" valign="top"><b>позиция Y в файле</td>');
                                        $this->_print('<td width="150px" valign="top"><b>файл с тайлом</td>');
                                        $this->_print('<td width="150px" valign="top"><b>категория</td>');
                                        $this->_print('<td width="150px" valign="top">&#160;</td>');
                                        $this->_print('</tr>');
                                        foreach($tiles as $tile)
                                        {
                                            $this->print_tile($tile);
                                        }
                                        $this->print_tile(array('posX'=>-1,'posY'=>-1));
                                        $this->_print('<td colspan="7" valign="top" ><input type="submit" value="save" name="save"/></td>');
                                        $this->_print('</table>');
                                        $this->_print('</form>');
                                        return $this->html;
                                    }
                                }
                                ?>
